package com.ruoyi.bussiness.service.impl;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bussiness.mapper.CcyAccountCofigMapper;
import com.ruoyi.bussiness.domain.CcyAccountCofig;
import com.ruoyi.bussiness.service.ICcyAccountCofigService;
import org.springframework.web.client.RestTemplate;

/**
 * 账号配置Service业务层处理
 * 
 * @author wzy
 * @date 2025-10-25
 */
@Service
@Slf4j
public class CcyAccountCofigServiceImpl implements ICcyAccountCofigService 
{
    @Autowired
    private CcyAccountCofigMapper ccyAccountCofigMapper;

    /**
     * 查询账号配置
     * 
     * @param id 账号配置主键
     * @return 账号配置
     */
    @Override
    public CcyAccountCofig selectCcyAccountCofigById(Long id)
    {
        return ccyAccountCofigMapper.selectCcyAccountCofigById(id);
    }

    /**
     * 查询账号配置列表
     * 
     * @param ccyAccountCofig 账号配置
     * @return 账号配置
     */
    @Override
    public List<CcyAccountCofig> selectCcyAccountCofigList(CcyAccountCofig ccyAccountCofig)
    {
        return ccyAccountCofigMapper.selectCcyAccountCofigList(ccyAccountCofig);
    }

    /**
     * 新增账号配置
     * 
     * @param ccyAccountCofig 账号配置
     * @return 结果
     */
    @Override
    public int insertCcyAccountCofig(CcyAccountCofig ccyAccountCofig)
    {
        int m = ccyAccountCofigMapper.insertCcyAccountCofig(ccyAccountCofig);
        sendOkxSystem();
        return m;
    }

    /**
     * 修改账号配置
     * 
     * @param ccyAccountCofig 账号配置
     * @return 结果
     */
    @Override
    public int updateCcyAccountCofig(CcyAccountCofig ccyAccountCofig)
    {
        int i = ccyAccountCofigMapper.updateCcyAccountCofig(ccyAccountCofig);
        sendOkxSystem();
        return i;
    }

    /**
     * 批量删除账号配置
     * 
     * @param ids 需要删除的账号配置主键
     * @return 结果
     */
    @Override
    public int deleteCcyAccountCofigByIds(Long[] ids)
    {
        int i = ccyAccountCofigMapper.deleteCcyAccountCofigByIds(ids);
        sendOkxSystem();
        return i;
    }

    /**
     * 删除账号配置信息
     * 
     * @param id 账号配置主键
     * @return 结果
     */
    @Override
    public int deleteCcyAccountCofigById(Long id)
    {
        int i = ccyAccountCofigMapper.deleteCcyAccountCofigById(id);
        sendOkxSystem();
        return i;
    }

    /**
     * 向OKX系统发送更新账号和交易规则指令
     */
    public void sendOkxSystem(){
        log.info("向OKX系统发送更新账号和交易规则指令");
        CompletableFuture.runAsync(() -> {
            try {
                RestTemplate restTemplate = new RestTemplate();
                String url = "http://43.135.143.197:8051/okx/web/tradeRout";
                String response = restTemplate.getForObject(url, String.class);
                log.info("异步调用交易规则接口成功，响应: {}", response);
            } catch (Exception e) {
                log.error("异步调用交易规则接口失败: {}", e.getMessage());
            }
        });
    }
}
